package com.CommunityHub.kiana.controller;



import com.CommunityHub.kiana.po.HouseInfo;
import com.CommunityHub.kiana.po.User;
import com.CommunityHub.kiana.service.MemberService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/ViewPurchasedHousesServlet")
public class ViewPurchasedHousesServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("user") == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        User user = (User) session.getAttribute("user");
        int memberId = user.getId();

        MemberService memberService = new MemberService();
        try {
            // 获取已购房产
            List<HouseInfo> purchasedHouses = memberService.getPurchasedHouses(memberId);
            request.setAttribute("purchasedHouses", purchasedHouses);

            request.getRequestDispatcher("viewPurchased.jsp").forward(request, response);
        } catch (SQLException e) {
            request.setAttribute("error", "加载已购房产失败: " + e.getMessage());
            request.getRequestDispatcher("viewPurchased.jsp").forward(request, response);
        }
    }
}
